home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / futilsrc.zoo / fileutil / src / mkdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-27  |  3.0 KB  |  126 lines

  1. /* mkdir -- make directories
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Options:
  19.    -p, +path        Ensure that the given path(s) exist:
  20.             Make any missing parent directories for each argument.
  21.             Parent dirs default to umask modified by `u+wx'.
  22.             Do not consider an argument directory that already
  23.             exists to be an error.
  24.    -m, +mode=mode    Set the mode of created directories to `mode', which is
  25.             symbolic as in chmod and uses the umask as a point of
  26.             departure.
  27.  
  28.    David MacKenzie <djm@ai.mit.edu>  */
  29.  
  30. #include <stdio.h>
  31. #include <getopt.h>
  32. #include <sys/types.h>
  33. #include "system.h"
  34. #include "modechange.h"
  35.  
  36. int make_path ();
  37. void error ();
  38. void strip_trailing_slashes ();
  39. void usage ();
  40.  
  41. /* If nonzero, ensure that a path exists.  */
  42. int path_mode;
  43.  
  44. /* The name this program was run with. */
  45. char *program_name;
  46.  
  47. struct option longopts[] =
  48. {
  49.   {"mode", 1, NULL, 'm'},
  50.   {"path", 0, &path_mode, 1},
  51.   {NULL, 0, NULL, 0}
  52. };
  53.  
  54. void
  55. main (argc, argv)
  56.      int argc;
  57.      char **argv;
  58. {
  59.   unsigned int newmode;
  60.   unsigned int parent_mode;
  61.   struct mode_change *change;
  62.   char *symbolic_mode;
  63.   int errors = 0;
  64.   int optc;
  65.  
  66.   program_name = argv[0];
  67.   path_mode = 0;
  68.   symbolic_mode = NULL;
  69.  
  70.   while ((optc = getopt_long (argc, argv, "pm:", longopts, (int *) 0)) != EOF)
  71.     {
  72.       switch (optc)
  73.     {
  74.     case 0:            /* Long option. */
  75.       break;
  76.     case 'p':
  77.       path_mode = 1;
  78.       break;
  79.     case 'm':
  80.       symbolic_mode = optarg;
  81.       break;
  82.     default:
  83.       usage ();
  84.     }
  85.     }
  86.  
  87.   if (optind == argc)
  88.     usage ();
  89.   
  90.   newmode = 0777 & ~umask (0);
  91.   parent_mode = newmode | 0300;    /* u+wx */
  92.   if (symbolic_mode)
  93.     {
  94.       change = mode_compile (symbolic_mode, 0);
  95.       if (change == MODE_INVALID)
  96.     error (1, 0, "invalid mode");
  97.       else if (change == MODE_MEMORY_EXHAUSTED)
  98.     error (1, 0, "virtual memory exhausted");
  99.       newmode = mode_adjust (newmode, change);
  100.     }
  101.  
  102.   for (; optind < argc; ++optind)
  103.     {
  104.       strip_trailing_slashes (argv[optind]);
  105.       if (path_mode)
  106.     errors |= make_path (argv[optind], newmode, parent_mode, -1, -1, NULL);
  107.       else if (mkdir (argv[optind], newmode))
  108.     {
  109.       error (0, errno, "cannot make directory `%s'", argv[optind]);
  110.       errors = 1;
  111.     }
  112.     }
  113.  
  114.   exit (errors);
  115. }
  116.  
  117. void
  118. usage ()
  119. {
  120.   fprintf (stderr, "\
  121. Usage: %s [-p] [-m mode] [+path] [+mode=mode] dir...\n",
  122.        program_name);
  123.   exit (1);
  124. }
  125.  
  126.